﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
//using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Controls;
using BrainTechLLC.WPF;
using DirectoryViewModels;
using System.Windows.Forms;

namespace BrainTechLLC.FolderExplorer
{
    /// <summary>
    /// Interaction logic for DirectoriesView.xaml
    /// </summary>
    public partial class DirectoriesView : System.Windows.Controls.UserControl
    {
        public static readonly AddDirCommand AddDirCommand = new AddDirCommand();
        public static readonly RemoveDirCommand RemoveDirCommand = new RemoveDirCommand();

        protected bool _setFirst;

        public static readonly DependencyProperty DirectoriesToExamineViewModelProperty =
            DependencyProperty.Register("DirectoriesToExamineViewModel", typeof(DirectoriesToExamineViewModel), typeof(DirectoriesView), new PropertyMetadata(null, OnDirectoriesToExamineViewModelPropertyChanged));

        public DirectoriesToExamineViewModel DirectoriesToExamineViewModel
        {
            get { return (DirectoriesToExamineViewModel)GetValue(DirectoriesToExamineViewModelProperty); }
            set { SetValue(DirectoriesToExamineViewModelProperty, value); }
        }

        private static void OnDirectoriesToExamineViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DirectoriesView source = d as DirectoriesView;
            DirectoriesToExamineViewModel v = e.NewValue as DirectoriesToExamineViewModel;
            source.UpdateDirectoriesToExamineViewModel(v);
        }

        private void UpdateDirectoriesToExamineViewModel(DirectoriesToExamineViewModel viewModel)
        {
            DirectoriesToExamineViewModel = viewModel;
            this.DataContext = DirectoriesToExamineViewModel;
            UpdateUI();
        }

        public static readonly DependencyProperty TextSizeProperty =
            DependencyProperty.Register("TextSize", typeof(double), typeof(DirectoriesView), new PropertyMetadata(9.0, OnTextSizePropertyChanged));

        public double TextSize
        {
            get { return (double)GetValue(TextSizeProperty); }
            set { SetValue(TextSizeProperty, value); }
        }

        private static void OnTextSizePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DirectoriesView source = d as DirectoriesView;
            double v = (double)e.NewValue;
            source.UpdateTextSize(v);
        }

        private void UpdateTextSize(double val)
        {
            UpdateUI();
        }

        public void UpdateUI()
        {
            lstItems.SetValue(TextBlock.FontSizeProperty, TextSize);
        }

        public DirectoriesView()
        {
            InitializeComponent();
        }

        public List<DirectoryToExamineViewModel> GetSelected()
        {
            return lstItems.GetSelected<DirectoryToExamineViewModel>();
        }
        
        private void RemoveDir(object sender, ExecutedRoutedEventArgs e)
        {
            var dirs = GetSelected();

            List<string> dirNames = new List<string>();

            foreach (var dir in dirs)
            {
                dirNames.Add(dir.Directory);
            }

            RemoveDirCommand.Execute(dirNames, DirectoriesToExamineViewModel);
        }

        private void AddDir(object sender, ExecutedRoutedEventArgs e)
        {
            string dir;

            if (!_setFirst)
            {
                _setFirst = true;
                dir = DirectoriesToExamineViewModel.Count == 0 ? Directory.GetCurrentDirectory() : DirectoriesToExamineViewModel[0].Directory;
            }
            else
            {
                dir = Directory.GetCurrentDirectory();
            }

            FolderBrowserDialog dialog = new FolderBrowserDialog()
            {
                SelectedPath = dir,
                ShowNewFolderButton = true,
                RootFolder = Environment.SpecialFolder.Desktop,
                Description = "Test"
            };

            var result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                AddDirCommand.Execute(DirectoriesToExamineViewModel, dialog.SelectedPath);                
                Directory.SetCurrentDirectory(dialog.SelectedPath);
            }
        }
    }
}
